﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class TypeBLL
    {
        private static int typeID = 0;
        GaraDataContext DB = new GaraDataContext();
        public IQueryable SelectAll()
        {
            return (from p in DB.Types select p).OrderByDescending(p => p.TypeID).AsQueryable();
        }

        public string Insert(TypeDTO typeDTO)
        {
            try
            {
                DAL.Type type = new DAL.Type();
                type.TypeID = typeID++;
                type.TypeName = typeDTO.TypeName;
                type.FeeRegister = typeDTO.FeeRegister;
                type.FeeExtension = typeDTO.FeeExtension;
                type.Status = typeDTO.Status;

                DB.Types.InsertOnSubmit(type);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Update(TypeDTO typeDTO)
        {
            try
            {
                DAL.Type type = DB.Types.Where(s => s.TypeID == typeDTO.TypeID).FirstOrDefault();
                type.TypeName = typeDTO.TypeName;
                type.FeeRegister = typeDTO.FeeRegister;
                type.FeeExtension = typeDTO.FeeExtension;
                type.Status = typeDTO.Status;

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public List<TypeDTO> FillDataCombobox()
        {
            return (from p in DB.Types
                    where p.Status == "Sử dụng"
                    select new TypeDTO{
            TypeID = p.TypeID,
            TypeName = p.TypeName
        }).ToList<TypeDTO>();
        }
    }
}
